package org.zn.mvcbase.mybatis.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 崔超
 * @date 2017年9月27日
 * @description boolean2number转换器
 * @version 1.00 beta
 */
@MappedJdbcTypes(JdbcType.SMALLINT)
@MappedTypes(boolean.class)
public class Number2Boolean extends BaseTypeHandler<Boolean> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)throws SQLException {
		if(parameter){
			ps.setShort(i, (short) 1);
		}else{
			ps.setShort(i, (short) 0);
		}
	}
	
	@Override
	public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
		short field = rs.getShort(columnName);
		if(field == 1){
			return true;
		}
		return false;
	}
	@Override
	public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		short field = rs.getShort(columnIndex);
		if(field == 1){
			return true;
		}
		return false;
	}
	@Override
	public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		short field = cs.getShort(columnIndex);
		if(field == 1){
			return true;
		}
		return false;
	}

	
	
	
	
}
